package co2.bean;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.hibernate.id.UUIDHexGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataIntegrityViolationException;

import co2.engine.BeanGetter;
import co2.engine.DAOInterface;
import co2.engine.SpringUtil;
import co2.pojos.MstRole;
import co2.pojos.MstUser;

@ManagedBean
@ViewScoped
public class RoleBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	@Qualifier("daoI")
	private DAOInterface dao;
	
	private MstRole mstRole;
	private MstRole selObj;
	private List<MstRole> listRole;
	
	@PostConstruct
	public void postCon(){
		mstRole = new MstRole();
		listRole = dao.getAllRole();
	}
	public void edit(){
		mstRole = selObj;
	}
	public void save(){ 
		try{
			 
				dao.save(mstRole);
				listRole = dao.getAllRole();
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Data saved",""));
			 
			
		}catch(DataIntegrityViolationException cve){
			cve.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"Role Id: "+mstRole.getId()+" sudah terpakai",""));
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error saving data",""));
		}finally{
			mstRole = new MstRole();
		}
	}
	
	public void saveUpdate(){ 
		try{
			 
			dao.update(mstRole);
			listRole = dao.getAllRole();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Data updated",""));
		 
		
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error updating data",""));
		}finally{
			mstRole = new MstRole();
		}
	}
	
	public void doDelete(){
		try{
			dao.delete(MstRole.class, selObj.getId());
			listRole = dao.getAllRole();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Data Deleted",""));
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error Deleting Data",""));
		}
	}
	public MstRole getMstRole() {
		return mstRole;
	}
	public void setMstRole(MstRole mstRole) {
		this.mstRole = mstRole;
	}

	public List<MstRole> getListRole() {
		return listRole;
	}

	public void setListRole(List<MstRole> listRole) {
		this.listRole = listRole;
	}

	public MstRole getSelObj() {
		return selObj;
	}

	public void setSelObj(MstRole selObj) {
		this.selObj = selObj;
	}
}
